//
//  ViewController.m
//  04-图片擦除
//
//  Created by Gavin on 15/9/21.
//  Copyright (c) 2015年 Gavin. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIImageView *imageV;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.imageV.userInteractionEnabled = YES;
    //添加手势
    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(pan:)];
    [self.imageV addGestureRecognizer:pan ];
}

//当手指在屏幕上拖动的时候调用.
- (void)pan:(UIPanGestureRecognizer *)pan{
    
    //获取当前的手指点.
    CGPoint curP = [pan locationInView:self.imageV];
    //确定擦除区域.
    CGFloat rectWH = 30;
    CGFloat x = curP.x - rectWH * 0.5;
    CGFloat y = curP.y - rectWH * 0.5;
    //确实擦除的位置尺寸.
    CGRect rect = CGRectMake(x, y, rectWH, rectWH);
    
    //开启一个位图上下文.
    UIGraphicsBeginImageContextWithOptions(self.imageV.bounds.size, NO, 0);
    
    //获取当前的上下文.
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    
    //把UIImageV上的图片渲染到上下文当中.
    [self.imageV.layer renderInContext: ctx];
    
    //设置擦除区域
    CGContextClearRect(ctx, rect);
    
    //生成一张新的图片.
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    
    //关闭位图上下文.
    UIGraphicsEndImageContext();
    
    //给图片重新赋值
    self.imageV.image = newImage;
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
